package com.library.books.service.impl;

import com.library.books.domain.vo.books.Cbc22Scale;
import com.library.books.domain.vo.books.CbcScaleCountVo;
import com.library.books.mapper.SysBooksCountMapper;
import com.library.books.mapper.SysCountMapper;
import com.library.books.service.ISysBooksCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Package: com.library.books.service.impl
 * @ClassName: SysBooksCountImpl
 * @Author: Yin
 * @Date: 10:01
 * @Description: 此文件描述信息
 */
@Service
public class SysBooksCountImpl implements ISysBooksCount {

    @Autowired
    private SysBooksCountMapper booksCountMapper;

    private String classOne = "A";
    private String classTwo = "B";
    private String classThree = "CDEFGHIJK";
    private String classFour = "NOPQRSTUVX";
    private String classFive = "Z";

    /**
     * 中图法分类统计-五大基本布类
     *  - 比例统计（金额，册数，ISBN类）
     */
    @Override
    public List<CbcScaleCountVo> chineseBookClassScaleCountBy5() {

        List<CbcScaleCountVo> result = new ArrayList<>();
        // 查询中国图书法分类22大类规划数据（类型-总金额，副本数量，isbn种类）
        List<Cbc22Scale> cbc22ScaleList = booksCountMapper.selectCbc22Scale();
        if(! cbc22ScaleList.isEmpty()) {
            // 总册数
            Long copyTotal = cbc22ScaleList.stream().map(Cbc22Scale::getCopyNumber).reduce(0L, Long::sum);
            // 总isbn种类
            Long isbnTotal = cbc22ScaleList.stream().map(Cbc22Scale::getIsbnNumber).reduce(0L, Long::sum);
            // 总价
            BigDecimal priceTotal = cbc22ScaleList.stream().map(Cbc22Scale::getTotalPrice).reduce(BigDecimal.ZERO, BigDecimal::add);
            // 统计
            List<Cbc22Scale> oneList = cbc22ScaleList.stream().filter(cbc -> classOne.contains(cbc.getBooksType())).collect(Collectors.toList());
            List<Cbc22Scale> twoList = cbc22ScaleList.stream().filter(cbc -> classTwo.contains(cbc.getBooksType())).collect(Collectors.toList());
            List<Cbc22Scale> threeList = cbc22ScaleList.stream().filter(cbc -> classThree.contains(cbc.getBooksType())).collect(Collectors.toList());
            List<Cbc22Scale> fourList = cbc22ScaleList.stream().filter(cbc -> classFour.contains(cbc.getBooksType())).collect(Collectors.toList());
            List<Cbc22Scale> fiveList = cbc22ScaleList.stream().filter(cbc -> classFive.contains(cbc.getBooksType())).collect(Collectors.toList());

            result.add(cbcScaleCountVoHandel(oneList,classOne,copyTotal,isbnTotal));
            result.add(cbcScaleCountVoHandel(twoList,classTwo,copyTotal,isbnTotal));
            result.add(cbcScaleCountVoHandel(threeList,classThree,copyTotal,isbnTotal));
            result.add(cbcScaleCountVoHandel(fourList,classFour,copyTotal,isbnTotal));
            result.add(cbcScaleCountVoHandel(fiveList,classFive,copyTotal,isbnTotal));
            result.add(new CbcScaleCountVo().setName("total").setTotalPrice(priceTotal).setCopyNumber(copyTotal).setIsbnNumber(isbnTotal));
        }
        return result;
    }

    /**
     * 中图法分类统计-二十二大分类
     *  - 比例统计（金额，册数，ISBN类）
     */
    @Override
    public List<CbcScaleCountVo> chineseBookClassScaleCountBy22() {
        List<CbcScaleCountVo> result = new ArrayList<>();
        // 查询中国图书法分类22大类规划数据（类型-总金额，副本数量，isbn种类）
        List<Cbc22Scale> cbc22ScaleList = booksCountMapper.selectCbc22Scale();
        if(! cbc22ScaleList.isEmpty()) {
            // 总册数
            Long copyTotal = cbc22ScaleList.stream().map(Cbc22Scale::getCopyNumber).reduce(0L, Long::sum);
            // 总isbn种类
            Long isbnTotal = cbc22ScaleList.stream().map(Cbc22Scale::getIsbnNumber).reduce(0L, Long::sum);

            for (Cbc22Scale cbc22Scale : cbc22ScaleList) {
                result.add(cbcScaleCountVoHandel(Arrays.asList(cbc22Scale),cbc22Scale.getBooksType(),copyTotal,isbnTotal));
            }

        }
        return result;
    }

    /**
     * 比例数据处理
     * @param list 中国图书法22大类比例数据
     * @param name 标签值
     * @param copyTotal  总 副本量
     * @param isbnTotal  总 ISBN 种类
     * @return 中国图书法分类 比例-统计
     */
    public CbcScaleCountVo cbcScaleCountVoHandel(List<Cbc22Scale> list,String name , Long copyTotal,Long isbnTotal) {
        CbcScaleCountVo one = new CbcScaleCountVo();
        one.setName(name);
        one.setCopyNumber(list.stream().map(Cbc22Scale::getCopyNumber).reduce(0L,Long::sum));
        one.setCopyNumberRatio(BigDecimal.valueOf(Double.valueOf(one.getCopyNumber()) / Double.valueOf(copyTotal) * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
        one.setIsbnNumber(list.stream().map(Cbc22Scale::getIsbnNumber).reduce(0L,Long::sum));
        one.setIsbnNumberRatio(BigDecimal.valueOf(Double.valueOf(one.getIsbnNumber()) / Double.valueOf(isbnTotal) * 100).setScale(2, RoundingMode.HALF_UP).doubleValue());
        one.setTotalPrice(list.stream().map(Cbc22Scale::getTotalPrice).reduce(BigDecimal.ZERO,BigDecimal::add));
        return one;
    }

}
